package com.central.apps.handler;

import com.central.app.common.base.BaseHandler;
import com.central.app.common.util.BLuntil;
import com.central.app.service.HandlerService;
import com.central.apps.handler.base.LvBaseHandler;
import com.central.common.auth.details.LoginAppUser;
import com.central.common.util.SysUserUtil;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;

/**
 * 点检标准处理类
 */

@Component
public class inspectionstandardHandler extends LvBaseHandler {
    @Autowired
    private HandlerService handlerService;

    /**
     * 数据保存前处理数据
     * @param app
     * @param objName
     * @param data
     * @param isUpdate
     * @throws Exception
     */
    @Override
    public void afterSave(String app, String objName, Map<String, Object> data, boolean isUpdate) throws Exception {
        if("bl_inspectionstandard".equals(objName)) {
            long id = MapUtils.getLongValue(data, "id");
            StringJoiner sj = new StringJoiner(",");
            //早班时间为空字符串时 将其置为null
            if (StringUtils.isBlank(MapUtils.getString(data, "morningshifttime"))) {
                sj.add(" morningshifttime = null ");
            }
            //中班时间为空字符串时 将其置为null
            if (StringUtils.isBlank(MapUtils.getString(data, "middleshifttime"))) {
                sj.add(" middleshifttime = null ");
            }
            //晚班时间为空字符串时 将其置为null
            if (StringUtils.isBlank(MapUtils.getString(data, "nightshifttime"))) {
                sj.add(" nightshifttime = null ");
            }
            if(StringUtils.isNotBlank(sj.toString())){
                handlerService.updateBySql("update bl_inspectionstandard set " + sj + " where id = '" + id + "'");
            }
            String insstdnum = MapUtils.getString(data, "insstdnum");//获取点检标准编号
            String operatoninspectioncycle = MapUtils.getString(data, "operatoninspectioncycle");//获取操作工点检周期
            if (operatoninspectioncycle!=null&&!operatoninspectioncycle.equals("")){
                if(operatoninspectioncycle.equals("S")){
                    handlerService.findBySql(" update bl_inspectionstandard set checkstarttime=NULL,checkcycleunit=NULL  where insstdnum='"+insstdnum+"'");
                }
                if (operatoninspectioncycle.equals("D")||operatoninspectioncycle.equals("W")||operatoninspectioncycle.equals("M")){
                    handlerService.updateBySql("update bl_inspectionstandard set  morningshifttime=NULL,middleshifttime=NULL,nightshifttime=NULL  where insstdnum='"+insstdnum+"'");
                }
            }
        }
    }
    //点检标准中可以选择所属区域
    @Override
    public Object operate(String app, String type, Map<String, Object> params) throws Exception {
        if ("getDept".equals(type)) {
            //获取登录用户信息
            LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
            //获取用户编号
            String userName = loginAppUser.getUsername();
            //判断前台传入的用户名参数是否为空
            if (userName != null && !"".equalsIgnoreCase(userName)) {
                //获取用户部门信息
                List<Map<String, Object>> list = handlerService.findBySql(
                        " select user.username,user.deptid  from sys_user user  " +
                                " LEFT JOIN sys_dept dept on  user.deptid=dept.code " +
                                " where user.username='"+userName+"'");
                String deptid = MapUtils.getString(list.get(0), "deptid"); //code
                String sql = "SELECT code,fullname,depttype  from sys_dept WHERE  LEFT(code,6)=LEFT('"+deptid+"',6) and depttype  in (3,4,5)";
                return BLuntil.toPage(params, handlerService, sql);
            }
        }
        if("getDeptFullname".equals(type)){
            //获取登录用户信息
            LoginAppUser loginAppUser = SysUserUtil.getLoginAppUser();
            //获取用户编号
            String userName = loginAppUser.getUsername();
            //判断前台传入的用户名参数是否为空
            if (userName != null && !"".equalsIgnoreCase(userName)) {
                //获取用户部门信息
                List<Map<String, Object>> list = handlerService.findBySql(
                        " select user.username,user.deptid  from sys_user user  " +
                                " LEFT JOIN sys_dept dept on  user.deptid=dept.code " +
                                " where user.username='"+userName+"'");
                String deptid = MapUtils.getString(list.get(0), "deptid"); //code
                String sql = "SELECT code,fullname  from sys_dept WHERE  LEFT(code,6)=LEFT('"+deptid+"',6) and depttype  in (3,4,5)";
                return BLuntil.toPage(params, handlerService, sql);
            }
        }
        return super.operate(app, type, params);
    }

}
